package controller;

import adapter.MessageAdapter;
import listener.MessageListener;
import org.json.JSONObject;
import test.JsonTest;
import util.MyClient;
import view.ChatPanel;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

/***
 * Author: Mr.Zhao, Time: 2018/5/24/11:25
 * Description: 用于控制聊天面板
 */
public class ChatPanelController extends MessageAdapter {

    private ChatPanel chatPanel;
    private JTextField textField;
    private JList<String> jList;
    private DefaultListModel<String> listModel;
    private JButton btn_send;
    private MyClient client;


    public ChatPanelController(ChatPanel chatPanel) {
        this.chatPanel = chatPanel;
        this.textField = chatPanel.getTextField();
        this.jList = chatPanel.getjList();
        this.listModel = new DefaultListModel<>();
        this.btn_send = chatPanel.getBtn_send();
        client = MyClient.getInstance();
        btn_send.setEnabled(false);
        btn_send.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String content = textField.getText();
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                String time = sdf.format(new Date());
                if (client.getOpNum() == -1) {
                    JOptionPane.showConfirmDialog(null, "对手玩家不存在！",
                            "发送消息失败", JOptionPane.DEFAULT_OPTION);
                    return;
                }
                listModel.addElement(String.format("我(%s)：%s", time, content));
                jList.setModel(listModel);

                JSONObject json = new JSONObject();
                json.put("flag", "聊天");
                json.put("time", time);
                json.put("content", content);
                client.sendMessage(json.toString());
                textField.setText("");
            }
        });

        textField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                disableBtnIfContentIsEmpty(e);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                disableBtnIfContentIsEmpty(e);
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });
    }

    /**
     * 输入框为空时，设为按钮为不可点击
     *
     * @param e textFiled中的监听事件
     */
    private void disableBtnIfContentIsEmpty(DocumentEvent e) {
        try {
            Document doc = e.getDocument();
            String str = doc.getText(0, doc.getLength());
            if (str.equals("")) {
                btn_send.setEnabled(false);
            } else {
                btn_send.setEnabled(true);
            }
        } catch (Exception exc) {
            exc.printStackTrace();
        }
    }


    @Override
    public void receivedChatMsg(String time, String content) {
        listModel.addElement(String.format("对手(%s)：%s", time, content));
        jList.setModel(listModel);
    }

    @Override
    public void gameStart(Color myColor) {
        listModel.clear();
        jList.setModel(listModel);
    }
}
